package com.mofang.activity.user;

import java.io.File;
import java.io.IOException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mofang.R;
import com.mofang.activity.BaseTitleViewActiviey;
import com.mofang.activity.login.LoginActivity;
import com.mofang.utils.TakePicture;
import com.mofang.widget.SelectPicPopupWindow;
import com.pactera.framework.util.CacheDir;
import com.pactera.framework.util.Loger;
import com.pactera.framework.util.TimeHelper;

public class UserSettingActivity extends BaseTitleViewActiviey implements
		OnClickListener {
	private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
	private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
	private static final int PHOTO_REQUEST_CUT = 3;// 结果

	private LinearLayout changeHeadImg_ly;// 修改头像
	private LinearLayout changeName_ly; // 修改姓名
	private LinearLayout maxCard_ly; // 二维码名片
	private LinearLayout changePsw_ly; // 修改密码
	private Button btn_loginOut; // 退出登录

	private ImageView iv_user_setting_user_face;

	// 自定义的弹出框类
	SelectPicPopupWindow menuWindow;
	File tempFile;

	TakePicture takePicture;

	// 使用系统当前日期加以调整作为照片的名称
	private String getPhotoFileName() {
		return TimeHelper.getCurrentDateStr("'IMG'_yyyyMMdd_HHmmss") + ".jpg";
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_user_setting);
		super.onCreate(savedInstanceState);
		setNavigationTitle("个人设置");
		setNavigationLeftBtnImage(R.drawable.return_arrow);
		takePicture = new TakePicture(UserSettingActivity.this);
		tempFile = new File(new CacheDir().getCameraPictureDirCache(),
				getPhotoFileName());
		if (!tempFile.exists()) {
			try {
				tempFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.user_setting, menu);
		return true;
	}

	@Override
	protected void initView() {
		super.initView();
		iv_user_setting_user_face = (ImageView) findViewById(R.id.iv_user_setting_user_face);
		changeHeadImg_ly = (LinearLayout) findViewById(R.id.user_setting_changeHeadImg_ly);
		changeHeadImg_ly.setOnClickListener(this);

		changeName_ly = (LinearLayout) findViewById(R.id.user_setting_changeName_ly);
		changeName_ly.setOnClickListener(this);

		maxCard_ly = (LinearLayout) findViewById(R.id.user_setting_maxCard_ly);
		maxCard_ly.setOnClickListener(this);

		changePsw_ly = (LinearLayout) findViewById(R.id.user_setting_changePsw_ly);
		changePsw_ly.setOnClickListener(this);

		btn_loginOut = (Button) findViewById(R.id.btn_loginOut);
		btn_loginOut.setOnClickListener(this);
	}

	// 为弹出窗口实现监听类
	private OnClickListener itemsOnClick = new OnClickListener() {

		public void onClick(View v) {
			menuWindow.dismiss();
			switch (v.getId()) {
			case R.id.btn_take_photo:
				// 照相
				// 调用系统的拍照功能
				takePicture.takePhoto(PHOTO_REQUEST_TAKEPHOTO, tempFile);
				break;
			case R.id.btn_pick_photo:
				// 选择照片
				takePicture.pickPhoto(PHOTO_REQUEST_GALLERY);
				break;
			default:
				break;
			}

		}

	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (requestCode) {
		case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用
			takePicture.startPhotoZoom(PHOTO_REQUEST_CUT,
					Uri.fromFile(tempFile), 150);
			break;

		case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时
			// 做非空判断，当我们觉得不满意想重新剪裁的时候便不会报异常，下同
			if (data != null)
				takePicture.startPhotoZoom(PHOTO_REQUEST_CUT, data.getData(),
						150);
			break;

		case PHOTO_REQUEST_CUT:// 返回的结果
			if (data != null)
				setPicToView(data);
			if (tempFile.exists())
				tempFile.delete();
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);

	}

	// 将进行剪裁后的图片显示到UI界面上
	private void setPicToView(Intent picdata) {

		Bundle bundle = picdata.getExtras();
		if (bundle != null) {
			Loger.d("--设置图片--");
			Bitmap photo = bundle.getParcelable("data");
			Drawable drawable = new BitmapDrawable(photo);
			iv_user_setting_user_face.setBackgroundDrawable(drawable);
		}
	}

	@Override
	public void onClick(View v) {

		Intent intent;
		switch (v.getId()) {

		case R.id.user_setting_changeHeadImg_ly:
			menuWindow = new SelectPicPopupWindow(UserSettingActivity.this,
					itemsOnClick);
			// 显示窗口
			menuWindow.showAtLocation(
					UserSettingActivity.this.findViewById(R.id.ll_root_view),
					Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置

			break;

		case R.id.user_setting_changeName_ly:
			intent = new Intent(getApplicationContext(),
					UserChangeNameActivity.class);
			startActivity(intent);

			break;
		case R.id.user_setting_maxCard_ly:
			intent = new Intent(getApplicationContext(),
					MyMaxCardActivity.class);
			startActivity(intent);
			break;
		case R.id.user_setting_changePsw_ly:
			intent = new Intent(getApplicationContext(),
					UserChangePasswordActivity.class);
			startActivity(intent);
			break;

		case R.id.btn_loginOut:
			// TODO 退出登录
			intent = new Intent(getApplicationContext(), LoginActivity.class);
			startActivity(intent);
			finish();
			break;

		}
	}

}
